<template>
  <view>
    <!-- 搜索 -->
    <search />
    <!-- 分类 -->
    <view class="category">

      <!-- 一级分类 -->
      <view class="sup">
        <!-- scroll-view:小程序中实现 区域滚动 时，使用的组件 -->
        <!-- scroll-y：可在y轴上(即：竖向滚动) 实现滚动效果-->
        <scroll-view scroll-y>
          <text 
          v-for="(one,index) in categoryList" 
          :key="one.cat_id" 
          @tap="change(index)"
          :class="{'active':(index==ac_index?true:false)}">{{one.cat_name}}</text>
        </scroll-view>
      </view>


      <!-- 二级分类 -->
      <view class="sub">
        <scroll-view scroll-y>
          <!-- 封面图 -->
          <image src="http://static.botue.com/ugo/uploads/category.png" class="thumb"></image>
          <view class="children" v-for="two in categoryList[ac_index].children" :key="two.cat_id">
            <view class="title">{{two.cat_name}}</view>

            <!-- 三级分类 -->
            <!-- 品牌 -->
            <view class="brands">
              <navigator url="/pages/list/index" v-for="three in two.children" :key="three.cat_id">
                <image :src="three.cat_icon"></image>
                <text>{{three.cat_name}}</text>
              </navigator>
            </view>

          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
  import search from '@/components/search';

  export default {
    data(){
      return {
        categoryList:[], // 全部商品的数据信息

        ac_index:0 // 默认显示一级分类的 第一项
      }
    },
    
    components: {
      search
    },

    methods:{
      // 获取分类页面的商品数据-----------------------------------------
      async getCategoryList(){
        const res = await this.request({
          url:"/api/public/v1/categories"
        })

        // console.log(res)
        this.categoryList = res
      },

      // 点击 一级分类 时执行的事件函数--------------------------------------
      change(index){
        this.ac_index = index
      }
    },

    onLoad(){
      this.getCategoryList()
    }

  }
</script>

<style scoped lang="less">
  scroll-view {
    height: 100%;
  }

  .category {
    display: flex;
    width: 100%;
    position: absolute;
    top: 100rpx;
    bottom: 0;

    .sup {
      width: 196rpx;
      background-color: #f4f4f4;

      text {
        display: block;
        height: 100rpx;
        text-align: center;
        line-height: 100rpx;
        font-size: 27rpx;
        color: #333;
        border-bottom: 1rpx solid #eee;

        &:last-child {
          border-bottom: none;
        }
        
        &.active {
          background-color: #FFF;
          color: #ea4451;
          position: relative;

          &::before {
            content: '';
            display: block;
            width: 8rpx;
            height: 60rpx;
            transform: translateY(-50%);
            background-color: #ea4451;

            position: absolute;
            left: 0;
            top: 50%;
          }
        }
      }
    }

    .sub {
      flex: 1;
      padding: 20rpx 18rpx;

      .thumb {
        width: 100%;
        height: 180rpx;
      }

      .children {
        text-align: center;
        color: #333;

        .title {
          display: inline-block;
          margin: 40rpx 0 20rpx;
          font-size: 30rpx;

          &::before {
            content: '/';
            margin-right: 20rpx;
            color: #666;
          }

          &::after {
            content: '/';
            margin-left: 20rpx;
            color: #666;
          }
        }
      }

      .brands {
        display: flex;
        flex-wrap: wrap;

        navigator {
          width: 33%;
          margin-bottom: 20rpx;
        }

        image {
          width: 120rpx;
          height: 120rpx;
        }

        text {
          display: block;
          font-size: 24rpx;
        }
      }
    }
  }
</style>
